package com.shopoo.behavior.app;

import java.util.List;
import java.util.stream.Collectors;

import jakarta.annotation.Resource;

import com.shopoo.behavior.api.ShareService;
import com.shopoo.behavior.app.converter.AppConverter;
import com.shopoo.behavior.dto.clientobject.BehaviorShareCO;
import com.shopoo.behavior.dto.cqe.BehaviorShareAddCmd;
import com.shopoo.behavior.infrastructure.database.dataobject.TBehaviorShare;
import com.shopoo.behavior.infrastructure.database.mapper.ShareMapper;
import com.szmengran.cola.dto.MultiResponse;
import com.szmengran.cola.dto.Response;

import org.springframework.stereotype.Service;

/**
 * File Name:ShareServiceImpl.java Package
 * Name:com.szmengran.behavior.service.impl
 * 
 * @TODO:
 * @author <a href="mailto:android_li@sina.cn">LiMaoYuan</a> Copyright (c) 2019,
 *         深圳市梦燃科技有限公司 All Rights Reserved.
 * @createTime 2019年6月3日下午5:02:51
 */
@Service
public class ShareServiceImpl implements ShareService {

    @Resource
    private ShareMapper<TBehaviorShare> shareMapper;

    @Override
    public Response addRead(String promotionid, String userid) {
        shareMapper.addLike(promotionid, userid);
        return Response.buildSuccess();
    }

    @Override
    public Response addLike(String promotionid, String userid) {
        shareMapper.addLike(promotionid, userid);
        return Response.buildSuccess();
    }

    @Override
    public Response cancelLike(String promotionid, String userid) {
        shareMapper.cancelLike(promotionid, userid);
        return Response.buildSuccess();
    }

    @Override
    public MultiResponse<BehaviorShareCO> findByPromotionid(String promotionid) {
        List<TBehaviorShare> list = shareMapper.findByPromotionid(promotionid);
        return MultiResponse.of(list.stream().map(tBehaviorShare -> {return AppConverter.INSTANCE.toBehaviorShareCO(tBehaviorShare);}).collect(Collectors.toList()));
    }

    @Override
    public Response insert(BehaviorShareAddCmd behaviorShareAddCmd) {
        TBehaviorShare share = AppConverter.INSTANCE.toTBehaviorShare(behaviorShareAddCmd);
        share.setReadCount(0);
        share.setLikeCount(0);
        shareMapper.insert(share);
        return Response.buildSuccess();
    }
}
